<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="Title" style="width: 200px;" class="filter-item" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        导出
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="ID" prop="id" sortable="custom" align="center" min-width="80px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="name" sortable="custom" align="center" width="120px">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账套类型" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.dbType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务器地址" width="150px" align="center">
        <template slot-scope="{row}">
          <span style="color:red;">{{ row.serverAddress }}</span>
        </template>
      </el-table-column>
      <el-table-column label="端口" width="80px" align="center">
        <template slot-scope="{row}">
          <span style="color:red;">{{ row.serverPort }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录名" width="110px" align="center">
        <template slot-scope="{row}">
          <span style="color:red;">{{ row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据库名称" width="110px" align="center">
        <template slot-scope="{row}">
          <span style="color:red;">{{ row.dataBaseName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="连接字符串" min-width="200px" align="center" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.connectingString }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini">
            编辑
          </el-button>
          <el-button v-if="row.isStart==0" size="mini" type="success" @click="handleEnable(row,$index)">
            启用
          </el-button>
          <el-button v-if="row.isStart==1" size="mini" type="success">
            禁用
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { getOrgs, enableOrg } from '@/api/org'
import waves from '@/directive/waves'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
export default {
  name: 'Org',
  directives: { waves },
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      downloadLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getOrgs().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    handleEdit(index, row) {
      console.log(index, row)
    },
    handleEnable(row, index) {
      enableOrg({
        ids: row.id,
        changeTo: 1
      }).then(response => {
        this.getList()
      })
    },
    handleDelete(index, row) {
      console.log(index, row)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      console.log('2')
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>
